package deno2;

public class MyQueue {
    int front;
    int rear;
    int[] elem;

    public MyQueue() {
        elem = new int[4];
    }

    //入队
    public boolean offer(int data) {
        if(isFull()) {
            return false;
        }
        elem[rear] = data;
        rear = (rear + 1) % elem.length;
        return true;
    }

    //队列是否已满
    private boolean isFull() {
        return (rear + 1) % elem.length == front;
    }

    //队列是否为空
    public boolean isEmpty() {
        return rear == front;
    }

    //出队
    public int poll() {
        if(isEmpty()) {
            return -1;
        }
        int ret = elem[front];
        front = (front + 1) % elem.length;
        return ret;
    }

    //求元素个数
    public int size() {
        int count = 0;
        for (int i = front; i < rear; i++) {
            count++;
        }
        return count;
    }

    //获取队头元素
    public int peek() {
        if(isEmpty()) {
            return -1;
        }
        return elem[front];
    }
}
